﻿

(function ($) {


    $.extend({
        String: {
            trim: function () {
                return this.replace(/^\s+|\s+$/g, '');
            },

            //if( $.StringEx.isNullOrEmpty('') ) alert('Empty string');
            isNullOrEmpty: function (value) {
                if (value) {
                    if (typeof (value) == 'string') {
                        if (value.length > 0)
                            return false;
                    }
                    if (value != null)
                        return false;
                }
                return true;

            },

            replace: function (source, oldVal, newVal) {
                var result = source;
                result = result.replace(new RegExp('\\' + oldVal + '\\', 'g'), newVal);
                return result;
            },

            format: function (format) {
                var result = format;
                for (var i = 1; i < arguments.length; i++) {
                    result = result.replace(new RegExp('\\{' + (i - 1) + '\\}', 'g'), arguments[i]);
                }
                return result;
            },
            startsWith: function (prefix, ignoreCase) {
                if (!prefix) return false;
                if (prefix.length > this.length) return false;
                if (ignoreCase) {
                    if (ignoreCase == true) {
                        return (this.substr(0, prefix.length).toUpperCase() == prefix.toUpperCase());
                    }
                }
                return (this.substr(0, prefix.length) === prefix);
            },
            endsWith: function (suffix, ignoreCase) {
                if (!suffix) return false;
                if (suffix.length > this.length) return false;
                if (ignoreCase) {
                    if (ignoreCase == true) {
                        return (this.substr(this.length - suffix.length).toUpperCase() == suffix.toUpperCase());
                    }
                }
                return (this.substr(this.length - suffix.length) === suffix);
            }
        }

    });


})(jQuery);


